package com.qfxl.common.config.webMvcConfig;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    //    @Resource
//    PublicInterceptor publicInterceptor;
    @Value("${startTokenIntercept:false}")
    boolean startTokenIntercept;
    @Resource
    TokenInterceptor tokenInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //添加不拦截路径
        List<String> excludePathPatterns = new ArrayList<>();
        excludePathPatterns.add("/source/**");
        excludePathPatterns.add("/user/login");
        excludePathPatterns.add("/static/**");
        if (startTokenIntercept) {
            registry.addInterceptor(tokenInterceptor)
                    .addPathPatterns("/**")//拦截所有的路径
                    //添加不拦截路径
                    .excludePathPatterns(excludePathPatterns);
        }

    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedMethods("GET", "POST")
                //允许携带头信息（该处为所有头信息，可根据自己的需求修改）
                .allowedHeaders("*")
                .allowedOrigins("*")
                .maxAge(3600);
    }
}